//
//  PlayViewController.m
//  Koala_Fm
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PlayViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <UIImageView+WebCache.h>
#import "AFNetBlock.h"
#import "PlayerModel.h"
#import "DataModel.h"
#import "BarModel.h"
#import "Player.h"

@interface PlayViewController ()
@property(nonatomic, retain)Player *player;//播放器对象
@property(nonatomic, retain)AVPlayerItem *playerItem;
// 播放对象
@property(nonatomic, retain)AVPlayerLayer *playerLayer;
// 播放图层
@property(nonatomic, retain)UISlider *slider;// 进度条
@property(nonatomic, retain)UILabel *timeLabel;//时间显示
@property(nonatomic, retain)UIButton *button;// 播放按钮
@property(nonatomic, assign)BOOL isplay;//是否播放
@property(nonatomic, assign)CGFloat totalDuration;//总时长

@property(nonatomic, retain)UILabel *lable;
@property(nonatomic, retain)UILabel *titleLable;//小标题
@property(nonatomic, retain)UILabel *termLabel;
@property(nonatomic, retain)UILabel *contLabel;//内容
@end

@implementation PlayViewController
- (void)dealloc
{
    [_player release];
    [_playerItem release];
    [_playerLayer release];
    [_slider release];
    [_timeLabel release];
    [_button release];
    [_lable release];
    [_titleLable release];
    [_termLabel release];
    [_contLabel release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    /** 创建播放器 **/
    [self creatAVPlayer];
    /** 进度条监听 **/
    [self progressObserving];
    
    
    //图像显示设置
    UIImageView *imag = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    imag.backgroundColor = [UIColor blackColor];
    [self.view addSubview:imag];
    [imag release];
    [imag sd_setImageWithURL:[NSURL URLWithString:_model.albumPic]];
    // 滑动控制器
    self.slider = [[UISlider alloc]initWithFrame:CGRectMake(0, 300, self.view.frame.size.width , 20)];
    _slider.backgroundColor = [UIColor blackColor];
    _slider.value =0;
    [self.view addSubview:_slider];
    //添加滑动事件
    [self.slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
    [_slider release];
    // 初始化时间显示
    self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 320, 150, 20)];
//    _timeLabel.backgroundColor = [UIColor redColor];
    [self.view addSubview:_timeLabel];
    _timeLabel.text = @"00.00/00.00";
    [_timeLabel release];
    // 播放按钮
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    _button.frame = CGRectMake(150, 330, 50, 50);
//    _button.backgroundColor = [UIColor blueColor];
    
    [self.button addTarget:self action:@selector(butt:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_button];
    [_button setImage:[UIImage imageNamed:@"bf"] forState:UIControlStateNormal];
    //lable设置
    self.lable = [[UILabel alloc]initWithFrame:CGRectMake(90, 380, 30, 30)];
//    self.lable.backgroundColor = [UIColor redColor];
    _lable.text = [_model.orderNum stringValue];
    // 字体居中显示
    _lable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_lable];
    [_lable release];
    self.titleLable = [[UILabel alloc]initWithFrame:CGRectMake(140, 380, 200, 30)];
//    self.titleLable.backgroundColor = [UIColor yellowColor];
    _titleLable.text = _model.audioName;
    [self.view addSubview:_titleLable];
    [_titleLable release];
    self.termLabel = [[UILabel alloc]initWithFrame:CGRectMake(120, 380, 30, 30)];
//    self.termLable.backgroundColor = [UIColor blueColor];
    _termLabel.text = @"期:";
    [self.view addSubview:_termLabel];
    [_termLabel release];
    
    self.contLabel = [[UILabel alloc]initWithFrame:CGRectMake(80, 410, 200, 30)];
    _contLabel.textAlignment = NSTextAlignmentCenter;
//    self.contLabel.backgroundColor = [UIColor greenColor];
    _contLabel.text = _strr;
    [self.view addSubview:_contLabel];
    [_contLabel release];
    
    
}

#pragma mark - 初始化播放器
- (void)creatAVPlayer{
    // 音频网址
    NSString *strr= _str;
    NSURL *url = [NSURL URLWithString:strr];
    // 创建播放对象
    self.playerItem = [AVPlayerItem playerItemWithURL:url];
    // 创建播放器
    self.player = [Player shareInstance:_playerItem];
   
    // 播放
    [_player play];
    
}
// 按钮实现方法
#pragma mark - 播放/暂停
- (void)butt:(UIButton *)sender{
  
    if (_isplay) {
        //正在播放时暂停
        [_player play];
        [sender setImage:[UIImage imageNamed:@"bf"] forState:UIControlStateNormal];
    }else{
        [_player pause];
        [sender setImage:[UIImage imageNamed:@"zt"] forState:UIControlStateNormal];
    }
    _isplay = !_isplay;
}

#pragma mark - 进度条监听
//// 分享按钮条监听
- (void)progressObserving{
    __strong PlayViewController *vc = self;
   [_player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
        //获取到总时长
       CGFloat duration = CMTimeGetSeconds(vc.playerItem.duration);
       //获取当前时长
       CGFloat current = CMTimeGetSeconds(vc.player.currentTime);
       //获取倒计时
       CGFloat rem = duration - current;
       // 剩余时间
       NSString *totalT = [NSString stringWithFormat:@"%02d:%02d",(int)rem / 60, (int)rem % 60];
       //当前时间
       NSString *currentT = [NSString stringWithFormat:@"%02d:%02d",(int)current / 60, (int)current % 60];
       // 拼接时间
       NSString *timeStr = [NSString stringWithFormat:@"%@/%@",currentT,totalT];
       vc.timeLabel.text = timeStr;
       // 保存总时长 用于slider手动控制进度
       vc.totalDuration = duration;
       //控制slider的当前进度
       vc.slider.value = current / duration;
       
    }];
    
}
#pragma mark - 手动进度控制
//滑动触发方法
- (void)sliderAction:(UISlider *)sender{
    //暂停
    [_player pause];
    // 当前时间
    CGFloat current = sender.value * _totalDuration;
    //创建CMTime
    CMTime time = CMTimeMake(current, 1);
    //把time赋值给播放器
    [_player seekToTime:time completionHandler:^(BOOL finished) {
        [_player play];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
